package org.black.phoneUtility.sms.reply.app.fragment;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;

public class SmsReplyReplyTimesModeLimitedTimesDialogFragment extends
		DialogFragment implements DialogInterface.OnClickListener {
	public static final String TAG = SmsReplyReplyTimesModeLimitedTimesDialogFragment.class
			.getName();
	public static final String ARGUMENTS_REPLY_TIMES_MODE_LIMITED_TIMES = "arguments_reply_times_mode_limited_times";

	public interface SmsReplyReplyTimesModeLimitedTimesDialogInterface {
		public void onSmsReplyReplyTimesModeLimitedTimesDialogPositiveClick(
				int number);

		public void onSmsReplyReplyTimesModeLimitedTimesDialogNegativeClick();
	}

	private SmsReplyReplyTimesModeLimitedTimesDialogInterface smsReplyReplyTimesModeLimitedTimesDialogInterface;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyReplyTimesModeLimitedTimesDialogInterface = (SmsReplyReplyTimesModeLimitedTimesDialogInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					activity.toString()
							+ " must implement SmsReplyReplyTimesModeLimitedTimesDialogInterface");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		Bundle bundle = getArguments();
		int limitedTimes = bundle
				.getInt(ARGUMENTS_REPLY_TIMES_MODE_LIMITED_TIMES);
		if (limitedTimes < 1) {
			limitedTimes = 5;
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(
				R.layout.sms_reply_reply_times_mode_limited_times_dialog, null);
		NumberPicker numberPicker = (NumberPicker) view
				.findViewById(R.id.smsReplyReplyTimesModeLimitedTimesNumberPicker);
		numberPicker.setMinValue(1);
		numberPicker.setMaxValue(50);
		numberPicker.setValue(limitedTimes);
		builder.setTitle(
				R.string.sms_reply_reply_times_limited_times_dialog_title)
				.setView(view).setPositiveButton(android.R.string.ok, this)
				.setNegativeButton(android.R.string.cancel, this);
		return builder.create();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		switch (which) {
		case DialogInterface.BUTTON_POSITIVE:
			NumberPicker numberPicker = (NumberPicker) this
					.getDialog()
					.findViewById(
							R.id.smsReplyReplyTimesModeLimitedTimesNumberPicker);
			this.smsReplyReplyTimesModeLimitedTimesDialogInterface
					.onSmsReplyReplyTimesModeLimitedTimesDialogPositiveClick(numberPicker
							.getValue());
			break;
		case DialogInterface.BUTTON_NEGATIVE:
			this.smsReplyReplyTimesModeLimitedTimesDialogInterface
					.onSmsReplyReplyTimesModeLimitedTimesDialogNegativeClick();
			break;
		default:
			break;
		}

	}
}
